<template>
  <div class="timetable-warp">
    <el-form
      ref="form"
      :model="form"
      label-width="80px"
      style="margin-top: 12px"
    >
      <el-row>
        <el-col :span="24">
          <el-button
            type="success"
            style="float: right; margin-bottom: 22px;"
            @click="update('add')"
            >添加系别</el-button
          >
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column
        prop="dept_id"
        align="center"
        label="系别ID"
      ></el-table-column>
      <el-table-column
        prop="dept_name"
        align="center"
        label="系别名"
        width="180"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button @click="update('edit', scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button
            @click="deleteDept(scope.row)"
            type="text"
            size="small"
            style="color: #f56c6c"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <updatePop ref="updatePop" @ok="getDeptList" />
  </div>
</template>

<script>
import { deptList, deptDelete } from '@/api/dept'
import updatePop from './components/updateDept-pop.vue';
export default {
  name: 'dept',
  components: {
    updatePop
  },
  data() {
    return {
      form: {},
      tableData: []
    }
  },

  methods: {
    // 1. 获取用户列表
    getDeptList() {
      deptList({ ...this.form }).then(res => {
        if (res.result) {
          this.tableData = res.data
        }
      })
    },

    // 2. 重置
    reset() {
      this.form = {}
      this.getDeptList()
    },

    // 3. 注册/修改用户弹窗
    update(type, record) {
      this.$refs.updatePop.openMask({ type, ...record })
    },

    // 4. 删除用户
    deleteDept(record) {
      deptDelete({ dept_id: record.dept_id }).then(res => {
        if (res.result) {
          this.$message.success(res.message)
          this.getDeptList()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  },

  mounted() {
    this.getDeptList()
  }
}
</script>

<style lang="stylus" scoped></style>
